package com.soar.recruitment.module.home.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;

import com.soar.recruitment.R;
import com.soar.recruitment.common.ui.fragment.SOARBaseBackFragment;
import com.soar.recruitment.common.ui.view.ptr.PtrFrameInit;
import com.soar.recruitment.common.ui.view.ptr.RecyclerViewInit;
import com.soar.recruitment.common.ui.view.roundview.RoundTextView;
import com.soar.recruitment.module.home.dal.bean.RecommendModle;
import com.soar.recruitment.module.home.ui.adapter.RecommendAdapter;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by chriswen on 2017/6/12.
 */

public class RecommendFragment extends SOARBaseBackFragment {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.container_ptr_frame)
    PtrFrameLayout containerPtrFrame;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    RoundTextView rtv_next;

    private View mFooterView;
    private View mEmptyView;

    private HeaderAndFooterWrapper headerAndFooterWrapper;

    private RecommendAdapter mRecommendAdapter;
    private List<RecommendModle> mRecommendModleList;

    public static RecommendFragment newInstance() {
        Bundle args = new Bundle();
        RecommendFragment fragment = new RecommendFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initVariable() {

    }

    @Override
    public void initView(Bundle savedInstanceState) {
        toolbar.setTitle("招考推荐");
        initToolbarNav(toolbar);

        initRecycleView();

        LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
        mFooterView = layoutInflater.inflate(R.layout.footer_recommend, recyclerView, false);
        mRecommendAdapter = new RecommendAdapter(_mActivity, R.layout.item_recommend, mRecommendModleList);
        headerAndFooterWrapper = new HeaderAndFooterWrapper(mRecommendAdapter);
        recyclerView.setAdapter(headerAndFooterWrapper);

        rtv_next = ButterKnife.findById(mFooterView, R.id.rtv_next);
        rtv_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                start(RecommendDetailFragment.newInstance());
            }
        });
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecycleView() {
        PtrFrameInit.init(containerPtrFrame, mActivity, false);
        containerPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, recyclerView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {

            }
        });
        containerPtrFrame.disableWhenHorizontalMove(true);
        containerPtrFrame.autoRefresh(true);

        RecyclerViewInit.init(recyclerView, mActivity);
    }

    @Override
    public void initData() {
        mRecommendModleList = new ArrayList<>();
        RecommendModle recommendModle = null;
        for (int i = 0; i < 5; i++) {
            recommendModle = new RecommendModle();
            mRecommendModleList.add(recommendModle);
        }
        mRecommendAdapter.setData(mRecommendModleList);

        if (headerAndFooterWrapper.getFootersCount() > 0) {
            headerAndFooterWrapper.removeFootView(mFooterView);
        }
        headerAndFooterWrapper.addFootView(mFooterView);
    }
}